package com.inifitness.common.xxl;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * TODO: (sunjinfeng 2024-06-06)
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024-06-06 13:20
 */
@Component
@Log4j2
@RequiredArgsConstructor
public class XxlJobUtil {

  private final XxlJobClient xxlJobClient;
  private static final String AUTHOR = "单次任务";
  private static final int DEFAULT_JOB_GROUP = 2;
  private static final String DEFAULT_SCHEDULE_TYPE = "CRON";
  private static final String DEFAULT_GLUE_TYPE = "BEAN";
  private static final String DEFAULT_EXECUTOR_ROUTE_STRATEGY = "FIRST";
  private static final String DEFAULT_MISFIRE_STRATEGY = "DO_NOTHING";
  private static final String DEFAULT_EXECUTOR_BLOCK_STRATEGY = "SERIAL_EXECUTION";
  private static final String DEFAULT_REMARK = "GLUE代码初始化";
  private static final int DEFAULT_TIMEOUT = 0;
  private static final int DEFAULT_FAIL_RETRY_COUNT = 0;

  public void addJob(String jobDesc, String cron, String jobHandler,String param) {

    XxlJobInfo jobInfo = XxlJobInfo.builder()
        .author(AUTHOR)
        .jobGroup(DEFAULT_JOB_GROUP)
        .jobDesc(jobDesc)
        .scheduleType(DEFAULT_SCHEDULE_TYPE)
        .scheduleConf(cron)
        .glueType(DEFAULT_GLUE_TYPE)
        .executorHandler(jobHandler)
        .executorRouteStrategy(DEFAULT_EXECUTOR_ROUTE_STRATEGY)
        .executorParam(param)
        .misfireStrategy(DEFAULT_MISFIRE_STRATEGY)
        .executorBlockStrategy(DEFAULT_EXECUTOR_BLOCK_STRATEGY)
        .glueRemark(DEFAULT_REMARK)
        .executorTimeout(DEFAULT_TIMEOUT)
        .executorFailRetryCount(DEFAULT_FAIL_RETRY_COUNT)
        .build();
    xxlJobClient.addAndStart(jobInfo);
  }
}
